<?php
namespace app\user\model\user;

use app\common\model\BaseModel;
use app\course\logic\base\Course;
use app\user\logic\user\Account as User;

/**
 * 收藏模型
 */
class Collection extends BaseModel
{

    /**
     * 表名
     * @var string
     */
    protected $name = 'user_collection';

    /**
     * 设置自动写入时间
     * @var boolean
     */
    protected $autoWriteTimestamp = true;

    protected $updateTime = false;

    /**
     * 定义字段类型
     * @var array
     */
    protected $type = [
        'id'            => 'integer',
        'source_type'   => 'string',
        'source_id'     => 'integer',
        'create_time'   => 'integer',
    ];

/* 获取器 */


/* 查询范围 */

    // 资源类型
    public function scopeSourceType($query, $source_type = 'video')
    {
        if ( $source_type == 'course' ) {
            $query->where('source_type', 'IN', 'video,live,offline,classes');
        } else {
            $query->where('source_type', $source_type);
        }
    }

/* 关联 */

    public function course()
    {
        return $this->belongsTo(Course::class, 'source_id')->with(['cover_url'])->bind(['title','cover','price','course_type','course_type_text','sale_count','sale_count_market',"cover_url"]);
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
